package com.wsx.sky.http.slidwindow;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.http.slidwindow.SlidTest
 * @Description: TODO
 * @date 2019/03/05
 */
public class SlidTest {


    public static void main(String[] args) {

        int[] datas = {1,2,4,6,9,-1,20,-3,9,2,0,11,-9,22,-8,1,3,5,10,20,0,-9,-10,30};

        int son = 4;
        int length = datas.length;

        long time = System.currentTimeMillis();
        System.out.println(maxOne(datas, son, length) + " times:" + (System.currentTimeMillis() - time));

        time = System.currentTimeMillis();
        System.out.println(maxTwo(datas, son, length) + " times:" + (System.currentTimeMillis() - time));
    }

    private static int maxTwo(int[] datas, int son, int length) {
        int maxSum = 0;
        for (int k = 0; k < 1000; k++) {
            maxSum = 0;
            if (son > length) {
                return -1;
            }

            for (int i = 0; i < son; i++) {
                maxSum += datas[i];
            }

            int windowSum = maxSum;
            for (int i = son; i < length; i++) {
                windowSum += (datas[i] - datas[i - son]);
                maxSum = Math.max(windowSum, maxSum);
            }
        }
        return maxSum;
    }

    private static int maxOne(int[] datas, int son, int length) {
        int maxSum = 0;
        for (int k = 0; k < 1000; k++) {
            for (int i = 0; i < length - son + 1; i++) {
                int windowSum = 0;
                for (int j = 0; j < son; j++) {
                    windowSum += datas[i + j];
                }
                maxSum = Math.max(maxSum, windowSum);
            }
        }
        return maxSum;
    }
}
